package com.yishun.springboot.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yinquan.util.DataUtil;
import com.yishun.springboot.httpclient.Post8806;
import com.yishun.springboot.pojo.GenOneDataResults;
import com.yishun.springboot.pojo.GeneralRequestModel;

import lombok.extern.slf4j.Slf4j;

/**
 * 该类有两个方法，tsp环境监测设备根据设备号请求最近一条数据和请求历史数据转发和做了一点点封装请求，请求到8806端口
 * @author YS
 *
 */

@Slf4j
public class Forwarding {

	/**
	 * 请求一条数据
	 * @param Device_code
	 * @return
	 */
	public String getLearnData(String Device_code) {
		Map map = new HashMap();
		Post8806 p = new Post8806();
		GeneralRequestModel generalRequestModel = new GeneralRequestModel();// 请求集
		GenOneDataResults results = new GenOneDataResults(); // 结果集

		// 請求8806獲取實時數據
		generalRequestModel.setProjectId("6620");
//    	generalRequestModel.setDeviceCode("000000YKDZHJJC0000000420");
//    	generalRequestModel.setBeginTime("2020-11-11 12:11:15");
//    	generalRequestModel.setEndTime("2020-11-21 13:11:15");
//    	generalRequestModel.setPageNumber(2);
//    	generalRequestModel.setPageNumber(20);

		generalRequestModel.setDeviceCode(Device_code);
		results = p.AllRequest0086("/Yc/GetData", generalRequestModel);
//	    	map.put(Device_code.toString(), results.getData());
		map.put("Success", true);
		map.put("Message", "操作成功");
		map.put("Code", 200);
		map.put("Data", results.getData());

//    	results = p.AllRequest0086("/Pdx/GetData",generalRequestModel);

//    	 for( Object key:map.keySet()){
//             log.info( key );
//             log.info(map.get(key));
//         }

		String returns = JSON.toJSONString(map, SerializerFeature.MapSortField);
		log.info("查询结果集为：" + returns);
		return returns;

	}

	/**
	 * 请求历史数据
	 * @param Device_code
	 * @return
	 */
	public String GetHistoryData(String Device_code) {
		String currDateStr = DataUtil.GetCurrDateStr();
		String TodayAfter3MinDateStr = DataUtil.GetTodayAfterDateStr();
//		log.info("begin is:"+TodayAfter3MinDateStr);
		log.info("begin is:"+"2020-12-14 07:25:20");
		log.info("end is:"+currDateStr);
		Map map = new HashMap();
		Post8806 p = new Post8806();
		GeneralRequestModel generalRequestModel = new GeneralRequestModel();// 请求集
		GenOneDataResults results = new GenOneDataResults(); // 结果集

		// 請求8806獲取實時數據
		generalRequestModel.setProjectId("6620");
//		generalRequestModel.setDeviceCode("000000YKDZHJJC0000000420");
		generalRequestModel.setBeginTime(TodayAfter3MinDateStr);
		generalRequestModel.setEndTime(currDateStr);
		generalRequestModel.setPageNumber(2);
		generalRequestModel.setPageNumber(20);

		generalRequestModel.setDeviceCode(Device_code);
		results = p.AllRequest0086("/Yc/GetHistoryData", generalRequestModel);
		map.put("Success", true);
		map.put("Message", "操作成功");
		map.put("Code", 200);
		map.put("Data", results.getData());

//    	results = p.AllRequest0086("/Pdx/GetData",generalRequestModel);

//    	 for( Object key:map.keySet()){
//             log.info( key );
//             log.info(map.get(key));
//         }

		String returns = JSON.toJSONString(map, SerializerFeature.MapSortField);
		log.info("查询结果集为：" + returns);
		return returns;

	}

	
	public GenOneDataResults GetHistoryMainData(String Device_code) {
		String currDateStr = DataUtil.GetCurrDateStr();
		String TodayAfter3MinDateStr = DataUtil.GetTodayAfterDateStr();
		log.info("begin is:"+TodayAfter3MinDateStr);
		log.info("end is:"+currDateStr);
		Post8806 p = new Post8806();
		GeneralRequestModel generalRequestModel = new GeneralRequestModel();// 请求集
		GenOneDataResults results = new GenOneDataResults(); // 结果集

		// 請求8806獲取實時數據
		generalRequestModel.setProjectId("6620");
//		generalRequestModel.setDeviceCode("000000YKDZHJJC0000000420");
		generalRequestModel.setBeginTime(TodayAfter3MinDateStr);
		generalRequestModel.setEndTime(currDateStr);
		generalRequestModel.setPageNumber(2);
		generalRequestModel.setPageNumber(20);

		generalRequestModel.setDeviceCode(Device_code);
		results = p.AllRequest0086("/Yc/GetHistoryData", generalRequestModel);

//    	results = p.AllRequest0086("/Pdx/GetData",generalRequestModel);

//    	 for( Object key:map.keySet()){
//             log.info( key );
//             log.info(map.get(key));
//         }

		return results;

	}
	
	/**
	 * 先不管
	 * @param args
	 */
	public static void main(String[] args) {
//		Forwarding f = new Forwarding();
//		JSONObject jsonString = JSON.toJSONString((f.GetHistoryMainData("000000YKDZHJJC0000000396").getData().toString()));
//		GenOneDataResults gr = JSON.parseObject(jsonString,GenOneDataResults.class);
//		log.info();
	}
}
